package com.yeebo.controller.sys;

import com.alibaba.fastjson.JSONObject;
import com.yeebo.common.BaseController;
import com.yeebo.interceptor.Access;
import com.yeebo.utils.RedisUtils;
import com.yeebo.utils.ResponseUtils;
import com.yeebo.vo.Result;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping("redis/manage")
public class RedisManageController extends BaseController {

    @Resource
    private RedisUtils redisUtils;

    @Access
    @RequestMapping("/loadListData")
    @ResponseBody
    public Result loadListData(HttpServletRequest request) {
        String filterKeys = request.getParameter("filterKeys");
        String matchPattern = StringUtils.isBlank(filterKeys) ? "*" : filterKeys;
        return ResponseUtils.success(redisUtils.keys(matchPattern));
    }

    @Access
    @RequestMapping("/deleteValueByKey")
    @ResponseBody
    public Result deleteValueByKey(@RequestBody JSONObject requestBody,
                                   HttpServletRequest request) {
        String redisKey = requestBody.getString("redisKey");
        redisUtils.delete(redisKey);
        return ResponseUtils.success("删除成功");
    }

    @Access
    @RequestMapping("/getRedisInfo")
    @ResponseBody
    public Result getRedisInfo(HttpServletRequest request) {
        return ResponseUtils.success(redisUtils.getRedisInfo());
    }

}
